package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.Questioncontent;
import com.ebring.shareEducation.bean.Questions;
import com.ebring.shareEducation.dao.QuestioncontentsMapper;
import com.ebring.shareEducation.dao.QuestionsMapper;
import com.ebring.shareEducation.service.QuestionsService;
import com.ebring.shareEducation.utils.DateUtil;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class QuestionsServiceImpl implements QuestionsService {


    @Autowired
    private QuestionsMapper questionsMapper;

    @Autowired
    private QuestioncontentsMapper questioncontentsMapper;


    @Override
    public RspMsg<Questions> selectQuestions() {
        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List<Questions> questionsList = questionsMapper.selectquestion();
        if (questionsList.isEmpty() || questionsList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关数据");
            return rspMsg;
        }
        PageInfo<Questions> pageInfo = new PageInfo<Questions>(questionsList);
        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("questionsList", questionsList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;
    }

    @Override
    public RspMsg<List<Questions>> findQuestions(Questions questions) {
        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List<Questions> questionList = questionsMapper.findQuestionsBy(questions);
        if (questionList.isEmpty() || questionList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关订单");
            return rspMsg;
        }

        PageInfo<Questions> pageInfo = new PageInfo<>(questionList);

        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("questionList", questionList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;
    }

    @Override
    public RspMsg<Questions> insertquestion(String type,String title,String author,String language,String file,String content) {
        RspMsg rspMsg = new RspMsg();
        Questions questions=new Questions();
        Questioncontent questioncontent=new Questioncontent();
        questions.setPid(UuidUtils.getNumberId(10));
        questioncontent.setCid(questions.getPid());
        questions.setType(type);
        questions.setTitle(title);
        questions.setAuthor(author);
        questions.setCreateTime(DateUtil.getStringDate());
        questions.setLanguage(language);
        questions.setFile(file);
        questioncontent.setContent(content);
        int i = questionsMapper.insertSelective(questions);
        int a=questioncontentsMapper.insertSelective(questioncontent);

        if (i < 0||a<0) {
            rspMsg.setError();
            rspMsg.setMsg("插入失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("插入成功");
        return rspMsg;
    }
}
